<?php
namespace App\Models\Chain\PointCash;





use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;

/**
 * 检查积分是否充足
 * Class CreateCreditLogAction
 * @package App\Models\Chain\Identity
 */

class CheckCreditAction extends AbstractHandler
{
    private $params = array();

    protected $error = array('error' => '积分不足', 'code' => 7011);


    public function __construct($params)
    {
        $this->params = $params;
    }
//CreateUserCreditLogAction
    public function handleRequest()
    {
        if ($this->checkCredit($this->params) == true) {
            $this->setSuccessor(new UpdateUserCreditAction($this->params));
            return $this->getSuccessor()->handleRequest();
        } else {
            return $this->error;
        }
    }


    public function checkCredit($params)
    {
       //拿到用户总积分
        $total = UserCreditFactory::getUserCreditTotal($params['userId']);

        if($params['credit'] > $total)
        {
            return false;
        }

        return true;
    }


}